package com.haoxi.gateway.common;

import java.io.UnsupportedEncodingException;

public class Text {
    /**
     * 分解字符串成指定的字符串数组，radix是大小，content是字符串
     */
    public static String[] getString(int radix, String content) {
        int contentLength = 0;
        contentLength = content.length();
        try {
            content = new String(content.getBytes("8859_1"), "GB2312");
            contentLength = content.length();
            if (contentLength == 0) {
                content = new String(content.getBytes("UnicodeBig"), "GB2312");
                contentLength = content.length();
            }
        } catch (Exception e) {
        }
        int size = contentLength / radix;
        size = (contentLength - size * radix) > 0 ? size + 1 : size;
        String oneContent[] = new String[size];
        for (int i = 0; i < size; i++) {
            int start = i * radix;
            int end = (i + 1) * radix;
            if (end > contentLength)
                end = contentLength;
            oneContent[i] = content.substring(start, end);
        }
        return oneContent;
    }

    /**
     * 将字符串分隔成多条字符串
     * 
     * @param ii 字符串的编码格式，默认为8 ,
     * @param radix 分隔后的一条字符串的长度
     * @param content 需分隔的字符串内容
     * @return 分隔后的字符串数组
     */
    public static String[] getString(int ii, int radix, String content) {
        int contentLength = 0;
        contentLength = content.length();
        int size = contentLength / radix;
        size = (contentLength - size * radix) > 0 ? size + 1 : size;
        String oneContent[] = new String[size];
        for (int i = 0; i < size; i++) {
            int start = i * radix;
            int end = (i + 1) * radix;
            if (end > contentLength)
                end = contentLength;
            oneContent[i] = content.substring(start, end);
        }
        return oneContent;
    }

    /**
     * 将byte数组根据编码格式不同转换成不同编码格式的字符串
     * 
     * @param Msg_fmt 数组的编码格式
     * @param Msg_Content 需转换的数组
     * @return 转换后的字符串
     */
    public static String getText(int Msg_fmt, byte[] Msg_Content) {
        return getText(Msg_fmt, Msg_Content.length, Msg_Content);
    }

    /**
     * 将byte数组根据编码格式不同转换成不同编码格式的字符串
     * 
     * @param Msg_fmt 数组的编码格式
     * @param len 内容长度
     * @param Msg_Content 需转换的数组
     * @return 转换后的字符串
     */
    public static String getText(int msg_fmt, int len, byte[] msg_Content) {

        len = len & 0xff;
        byte[] tmp = new byte[len];
        System.arraycopy(msg_Content, 0, tmp, 0, tmp.length);
        String result = "";
        try {
            if (0 == msg_fmt) {
                result = new String(tmp);
            } else if (8 == msg_fmt) {
                result = new String(tmp, "UnicodeBig");
            } else if (15 == msg_fmt) {
                result = new String(tmp, "GBK");
            } else if (10 == msg_fmt) {
                result = new String(tmp, "UnicodeBig");
            } else {
                result = new String(tmp);
            }
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return result.trim();
        // len=len & 0xff;
        // byte b[] = new byte[len];
        // for(int i=0 ; i<len;i++)
        // {
        // b[i] = Msg_Content[i];
        // }
        // String s="";
        // try
        // {
        // if(Msg_fmt==0)
        // {
        // s=new String(b);
        // }
        // else if(Msg_fmt==8)
        // {
        // s=new String(b,"UnicodeBig");
        // }
        // else if(Msg_fmt==15)
        // {
        // s = new String(b, "GB2312");
        // }
        // else if(Msg_fmt==10)
        // {
        // s=new String(b,"UnicodeBig");
        // }
        // else
        // s=new String(b);
        // }catch(Exception e){
        // //MainSystem.logPrint("汉字转码有问题："+e);
        // e.printStackTrace();
        // }
        // return s.trim();
    }

    /**
     * 将字符串根据给出的编码格式转换成byte数组
     * 
     * @param dcs 字符串编码格式
     * @param s 需转换的字符串
     * @return 转换后的byte数组
     */
    public static byte[] textToByte(int dcs, String s) {
        byte[] tempbytes = null;
        if (dcs == 8)
            tempbytes = textToByte(s);
        else if (dcs == 0x18) {
            byte[] t = textToByte(s);
            tempbytes = new byte[t.length + 2];
            tempbytes[0] = 0x00;
            tempbytes[1] = 0x01;
            for (int i = 0; i < t.length; i++)
                tempbytes[i + 2] = t[i];
        } else if (dcs == 15)
            tempbytes = s.getBytes();
        else if (dcs == 4)
            tempbytes = convertStringToBytes(s);
        else
            tempbytes = textToByte(s);
        return tempbytes;
    }

    public static byte[] convertStringToBytes(String srcBuff) {
        byte dest[] = (byte[]) null;
        try {
            dest = new byte[srcBuff.length() / 2];
            int j = 0;
            for (int i = 0; i < srcBuff.length();) {
                String subBuff = srcBuff.substring(i, i + 2);
                byte ab = (byte) Integer.parseInt(subBuff, 16);
                dest[j] = ab;
                i += 2;
                j++;
            }

            return dest;
        } catch (Exception exception) {
            return dest;
        }
    }

    /**
     * 将字符串转换成unicode编码的字节数组
     * 
     * @param s 需转换的字符串
     * @return 转换后的unicode编码的数组
     */
    public static byte[] textToByte(String s) {
        // MainSystem.logPrint("dcs==8---汉字转码："+s);
        byte[] tempbytes = new byte[s.length() * 2];
        char[] c = s.toCharArray();
        int j = 0;
        for (int ei = 0; ei < c.length; ei++) {
            int d1 = c[ei] % 16;
            int d2 = (c[ei] / 16) % 16;
            int d3 = (c[ei] / (16 * 16)) % 16;
            int d4 = (c[ei] / (16 * 16 * 16)) % 16;
            String s1 = new StringBuffer(Integer.toHexString(d4)).append(
                    Integer.toHexString(d3)).toString();
            String s2 = new StringBuffer(Integer.toHexString(d2)).append(
                    Integer.toHexString(d1)).toString();
            tempbytes[j] = (byte) Integer.parseInt(s1, 16);
            tempbytes[j + 1] = (byte) Integer.parseInt(s2, 16);
            j += 2;
        }
        return tempbytes;
    }

    /**
     * 将unicode类型的byte数组转换成字符串
     * 
     * @param b 需转换的数组
     * @return 转换后的字符串
     */
    public static String byteToText(byte[] b) {
        char[] c = new char[b.length / 2];
        for (int i = 0, j = 0; i < c.length; i += 2) {
            String b1 = "00" + Integer.toHexString(b[i]);
            String b2 = "00" + Integer.toHexString(b[i + 1]);
            if (b1.length() > 2)
                b1 = b1.substring(b1.length() - 2, b1.length());
            if (b2.length() > 2)
                b2 = b2.substring(b2.length() - 2, b2.length());
            c[j] = (char) Integer.parseInt(b1 + b2, 16);
            j++;
        }
        return new String(c);
    }
    public static void main(String[] args)
    {
        try
        {
           byte[] b="1122中国你好".getBytes("ascii");
           System.out.println(b.length);
           for(int i=0;i<b.length;i++)
           {
               System.out.println(b[i]);
           }
           String s=new String(b,"gbk");
           System.out.println(s);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

}
